package com.bcurtu.amigo.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bcurtu.amigo.pojo.Amigo;
import com.bcurtu.amigo.services.AmigoInvisibleBO;
import com.bcurtu.amigo.services.EncodingException;
import com.bcurtu.amigo.services.GroupIdEncrypter;

public class DecoderController extends AbstractController {

	private String adminPwd;
	private AmigoInvisibleBO bo;
	
	 


	public AmigoInvisibleBO getBo() {
		return bo;
	}

	public void setBo(AmigoInvisibleBO bo) {
		this.bo = bo;
	}

	public String getAdminPwd() {
		return adminPwd;
	}

	public void setAdminPwd(String adminPwd) {
		this.adminPwd = adminPwd;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String ticket=arg0.getParameter("ticket");
		String pwd=arg0.getParameter("pwd");
		if (!(adminPwd.equals(pwd)))
			return new ModelAndView("decodeerror","ticket",ticket);		
		try{
			Map out=new HashMap();
			Integer id=GroupIdEncrypter.decode(ticket);
			List<Amigo> as=bo.recupera(id);
			out.put("id",id);
			out.put("amigos",as);
			return new ModelAndView("decodeok","out",out);
		}catch (EncodingException ee){
			return new ModelAndView("decodeerror","ticket",ticket);			
		}
	}

}
